<template>
  <div class="goods-hot">
    <h3>{{title}}</h3>
    <!-- 商品区块 -->
    <RouterLink to="/" class="goods-item" v-for="good in goodsHotList" :key="good.id">
      <img :src="good.picture" alt="" />
      <p class="name ellipsis">{{good.name}}</p>
      <p class="desc ellipsis">{{good.desc}}</p>
      <p class="price">&yen;{{good.price}}</p>
    </RouterLink>
  </div>
</template>

<script>
// 导入组合api
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
// 导入请求热榜数据的接口
import { goodsHot } from '@/api/goods/index.js'
export default {
  name: 'goodHot',
  props: {
    type: {
      type: Number,
      default: 1
    }
  },
  setup (props) {
    // 设置初始化标题数据
    const titleList = { 1: '24小时热销榜', 2: '周热销榜', 3: '总热销榜' }
    const title = computed(() => {
      return titleList[props.type]
    })
    // 初始化商品列表
    const goodsHotList = ref([])
    const route = useRoute()
    // 请求数据
    async function loadGoodsHotList () {
      const res = await goodsHot({ id: route.params.id, type: props.type })
      console.log(res, '热榜数据')
      goodsHotList.value = res.result
    }
    loadGoodsHotList()
    return {
      goodsHotList, title
    }
  }
}
</script>

<style lang="less" scoped>
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
  .goods-item {
    display: block;
    padding: 20px 30px;
    text-align: center;
    background: #fff;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
}
</style>
